// pages/agelist/agelist.js
var that,
  api = new (require('../../utils/api.js')),
  imghead = api.getimgurl(),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({
 

  /**
   * 页面的初始数据
   */
  data: {
   agelist:['3-4岁','4-5岁','5-6岁'],
   agescope:[{agefrom:3,ageto:4},{agefrom:4,ageto:5},{agefrom:5,ageto:6}],
    // navbar: ['默认', '英语', '中文','双语'],
  
    isCollect: false, 
    currentTab: 0,
    currentageindex:0,
    pagesize:5,
    list:[],
    start:0,
    currentpage:1,
  },
  getbookbyage(start){
    wx.showLoading({
      title: '加载中',
    })
    let { pagesize,currentageindex,agescope } = this.data, agefrom = 3,ageto=5;
    let data = agescope[currentageindex];
    data.start = start;data.pagesize = pagesize;
    
    api.getbookbyage(data,res=>{
      if(res.data.issuccess==1){
        let list = res.data.list,length=res.data.list.length;
        this.setData({length});
        this.formatdata(list);
      }
    });
  },
  formatdata(clist) {
    let {cartids} = this.data;
    clist.forEach((x, i) => {
      if (x.thumbPicPath.indexOf("http" == -1)) {
        x.thumbPicPath = imghead + x.thumbPicPath;
      }
      if (cartids.indexOf(x.id) != -1) {
        clist[i].already = !0;
      }
      x.collect = 0;
      x.detaillist = x.detaillist.split(',');
      x.labels = x.labels.split(',');
      if (x.detaillist.length > 0) {
        x.detaillist.forEach((y, i) => {
          if (y.indexOf("http") == -1) {
            x.detaillist[i] = imghead + x.detaillist[i];
          }
        });
      }
    });
    let {list} = this.data;
    list = list.concat(clist); 
    this.setData({ list });
    wx.hideLoading();
  },
  editcart(e) {
    let { index } = e.currentTarget.dataset, { list, cartids } = this.data,
      already = !list[index].already,
      ids = list[index].id;
    if (already) {
      api.addcart({ goodsid: ids }, res => {
        if (res.data.issuccess == 1) {
          list[index].already = already;
          wx.showToast({
            title: '添加成功',
          })
          cartids.push(ids);
          this.setData({ list, cartids });
          wx.setStorageSync("cartids", cartids);
          app.badege(cartids);
        }
      });
    } else {
      api.delcart({ ids }, res => {
        if (res.data.issuccess == 1) {
          list[index].already = already;
          wx.showToast({
            title: '取消成功',
          })
          let i = cartids.indexOf(ids);
          cartids.splice(i, 1);
         
          this.setData({ list, cartids });
          wx.setStorageSync("cartids", cartids);
          app.badege(cartids);
        }

      })
    }
  },
  toCollect(e) {
    let {index} = e.currentTarget.dataset,{list} = this.data;
  
    list[index]['collect']=list[index]['collect']==0?1:0;
    this.setData({list})
    
  },
  navbarTap: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })},
  //点击切换
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    that =this;
   
    let cartids = wx.getStorageSync("cartids");
    that.setData({ currentageindex:options.i||0,cartids});

    this.getbookbyage(0);
  },
  chose(e){
    let {currentageindex} = e.currentTarget.dataset;
    this.setData({currentageindex,list:[],start:0});
    
    this.getbookbyage(0);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let { pagesize, currentpage, start, length, currentageindex } = this.data;

    if (length != pagesize) {
      wx.showToast({
        icon: "none",
        title: '再怎么扯也没有了',
      })
      return;
    }
    start = pagesize * currentpage; currentpage = currentpage + 1;

    this.setData({ start, currentpage });
    this.getbookbyage(start);
  },
  todetail(e){
    let {index} = e.currentTarget.dataset;
    
    wx.navigateTo({
      url: '../books/detail/detail?i='+index,
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
 
})